package com.quzi.kettle.activitise.process;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.httprequest.AsyncBean;
import com.example.httprequest.AsyncHttpUtilClient;
import com.loopj.android.http.RequestParams;
import com.quzi.kettle.GlobalConfig;
import com.quzi.kettle.MyApplication;
import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.activitise.main.MainActivityTest;
import com.quzi.kettle.activitise.main.MyKettles;
import com.quzi.kettle.activitise.main.TempActivity;
import com.quzi.kettle.api.response.BooleanResponse;
import com.quzi.kettle.api.response.DeviceListResponse;
import com.quzi.kettle.bean.http.KettleDevice;
import com.quzi.kettle.view.TitleBarHandler;
import com.quzi.kettle.view.TitleBarView;

public class BindKettleActivity extends BaseActivity implements OnClickListener {

	private TitleBarView titleView;
	private Button confirm;
	private EditText kettle_name;

	private boolean isCheck = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configration3);

		kettle_name = (EditText) findViewById(R.id.config_passwd_input);
		confirm = (Button) findViewById(R.id.confirm);
		confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (kettle_name.getText().toString().trim().isEmpty()) {
					UIHandler.obtainMessage(TOASTMSG, "请输入水壶名称").sendToTarget();
				} else {

					// if (!isShowingDialog()) {
					// showLoadingDialog();
					// }
					Map<String, String> request = new HashMap<String, String>();
					request.put("main_mac", GlobalConfig.topic);
					request.put("name", kettle_name.getText().toString());
					AsyncHttpUtilClient.request(
							getApplicationContext(),
							new AsyncBean(
									AsyncHttpUtilClient.POST,
									MyApplication.url
											+ MyApplication.requestMap
													.get(MyApplication.DEVICE_BIND),
									MyApplication.header, new RequestParams(
											request)),
							new BooleanResponse<Boolean>() {

								@Override
								public void onSuccess(Boolean t) {

									AsyncHttpUtilClient
											.request(
													getApplicationContext(),
													new AsyncBean(
															AsyncHttpUtilClient.GET,
															MyApplication.url
																	+ MyApplication.requestMap
																			.get(MyApplication.DEVICE_LIST),
															MyApplication.header,
															null),
													new DeviceListResponse<List<KettleDevice>>() {

														@Override
														public void onSuccess(
																List t) {

															hideSoftInput();
															dissLoadingDialog();
															deviceList = t;
															currDevice = (KettleDevice) t
																	.get(0);
															nextActivity(MainActivityTest.class);
															// finish();
														}

														@Override
														public void onFailure(
																int arg0,
																String arg1) {

															dissLoadingDialog();
														}
													});

								}

								@Override
								public void onFailure(int arg0, String arg1) {
									dissLoadingDialog();
									UIHandler.obtainMessage(TOASTMSG, "failed")
											.sendToTarget();
								}
							});
				}
			}
		});

		initTitleBar();

	}

	/**
	 * 点击返回按键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {// 如果按下的是回退健
			// 表示按下回退键且不是常按
			if (event.getAction() == KeyEvent.ACTION_DOWN
					&& event.getRepeatCount() == 0) {
				if (deviceList.size() == 0) {
					GlobalConfig.topic="";
					nextActivity(TempActivity.class);
				} else {
					nextActivity(MyKettles.class);
				}
				// 调用上面写的退出

				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void initTitleBar() {
		// TODO Auto-generated method stub
		super.initTitleBar();
		titleView = (TitleBarView) findViewById(R.id.titleview);
		
		titleView.setTitleBarClickListener(new TitleBarHandler() {

			@Override
			public void ClickRight() {
			}

			@Override
			public void ClickLeft() {
				if (deviceList.size() == 0) {
					GlobalConfig.topic="";
					nextActivity(TempActivity.class);
				} else {
					finish();
				}

				//
			}
		});
	}

	@Override
	public void onClick(View view) {

	}

}
